package com.example.demo.utils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Base64Util {
    public static String getBase64(BufferedImage image) {
        String base64 = null;
        try {
            //输出流
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            ImageIO.write(image, "png", stream);
//            Base64.encode(stream.toByteArray());
            base64 = java.util.Base64.getEncoder().encodeToString(stream.toByteArray());
            //  log.info("生成的图片验证码base64:{}", base64);
        } catch (IOException e) {
            //  log.error("生成生成的图片验证码base64失败：{}", e.getMessage());
            e.printStackTrace();
        }
        return base64;
    }
}
